﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Employees_ContractDetail : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {



        if (!IsPostBack)
        {


            //if (Request["cid"] != null)
            //{
            //    WMaster.SetWhere("ContractId=" + Request["cid"].ToString());
            //}
            //else
            //{
            //    WMaster.SetWhere("ContractId=0");
            //}


            if (Request["action"] != null)
            {
                InitializeComponent(true);
                WMaster.SetWhere("Overdue=0 and Contract.EmployeeID=" + Request["eid"].ToString());
                Master.DataSource = WMaster;


            }
            else
            {
                InitializeComponent(true);

                Master.DataSource = WMaster;


            }
            ((HiddenField)this.Page.Master.FindControl("HoverId")).Value = "EmpContract";
         



            MasterPage_MasterPage_EmpDetail master = (MasterPage_MasterPage_EmpDetail)this.Page.Master;
            master.HelpHref = "";
            master.PageName = "员工合同";
            master.PicUrl = "~/image/PageIcon/hetong.jpg";
            master.IntiBar();


        }
    }

    private void InitializeComponent(bool action)
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Employees_ContractDetail));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = true;
        this.WMaster.AlwaysClose = true;
        this.WMaster.DataCompressed = false;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SEmployees.Contract";
        this.WMaster.ServerModify = true;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }
    protected void Contract_DataBound(object sender, EventArgs e)
    {
        //SelUser 

        try
        {

            if (Master != null)
            {
                Control_SelUser su = Contract.FindControl("SelUser") as Control_SelUser;

                hidEid.Value = Master.CurrentRow["EmployeeId"].ToString();

                if (Master.CurrentRow["EmpName"].ToString() == "")
                {
                    hidEname.Value = hidEname.Value;

                }
                else
                {
                    hidEname.Value = Master.CurrentRow["EmpName"].ToString();

                }

                su.ReadOnly(hidEid.Value, hidEname.Value);




                if (Request["action"].ToString() == "self" || Request["action"].ToString() == "view")
                {
                    wn.Visible = false;
                }
                else if (Master.CurrentRow["Contractbegin"].ToString() == "" || Master.CurrentRow["Contractend"].ToString() == "")
                {
                    wn.NavControls.Remove(wn.NavControls["Add"]);
                }
            }



        }
        catch
        {
            //string eid = Request["eid"].ToString();
            //object[] oj = Srvtools.CliUtils.CallMethod("SEmployees", "GetEmpNameByEid", new object[] { eid });
            //string ename = oj[1].ToString();

            //Control_SelUser su = Contract.FindControl("SelUser") as Control_SelUser;

            //su.ReadOnly(eid, ename);
            //wn.NavControls.Remove(wn.NavControls["Update"]);
            //wn.NavControls.Remove(wn.NavControls["Delete"]);
            if (Request["action"] != null)
            {
                try
                {
                    Control_SelUser su = Contract.FindControl("SelUser") as Control_SelUser;

                    su.ReadOnly(Master.CurrentRow["EmployeeId"].ToString(), Master.CurrentRow["EmpName"].ToString());
                }
                catch
                {
                    FieldContract.Visible = false;
                }


            }
            else
            {
                if (hidEid.Value != "")
                {


                    Control_SelUser su = Contract.FindControl("SelUser") as Control_SelUser;

                    su.ReadOnly(hidEid.Value, hidEname.Value);

                    readhistory.Visible = true;
                }
                else
                {
                    Control_SelUser su = Contract.FindControl("SelUser") as Control_SelUser;
                    su.SelUserInti("", "");
                    readhistory.Visible = false;
                }
            }

        }
        finally
        {
            if (Request["redurl"] != null)
            {
                backLink.Visible = true;
            }
            else
            {
                backLink.Visible = false;
            }
        }
        //MasterHr.CurrentRow["DepartmentId"].ToString()

    }
    protected void wnHr_Command(object sender, CommandEventArgs e)
    {


        if (e.CommandName == "cmdFirst" || e.CommandName == "cmdPrevious"
            || e.CommandName == "cmdNext" || e.CommandName == "cmdLast")
        {
            Master.ExecuteSelect(Contract);
            DataBind();
        }
        else if (e.CommandName == "cmdAdd")
        {
            //Master.ExecuteAdd(Contract);
            DataBind();
        }
        else if (e.CommandName == "cmdDelete")
        {
            Master.ExecuteSelect(Contract);
            DataBind();
        }
        else if (e.CommandName == "cmdApply" && Contract.AllValidateSucess)
        {
            Master.ExecuteSelect(Contract);
            DataBind();
        }
    }
    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect(Server.HtmlDecode(Request["redurl"].ToString()));
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string url = "ContractSelf.aspx?eid=" + hidEid.Value;
        Response.Redirect(url);
    }

    public string GetBegin()
    {
        DateTime Time = new DateTime();
        try
        {
            Time = Convert.ToDateTime(Master.CurrentRow["Contractend"]);
        }
        catch
        {
            Time = DateTime.Now;
        }
        return Time.AddDays(1).ToString("yyyy-MM-dd");
    }

    public string GetEnd()
    {
        DateTime Time = new DateTime();
        try
        {
            Time = Convert.ToDateTime(Master.CurrentRow["Contractend"]);
        }
        catch
        {
            Time = DateTime.Now;
        }
        return Time.AddYears(1).ToString("yyyy-MM-dd");
    }

    protected void Master_Adding(object sender, Srvtools.WebDataSourceAddingEventArgs e)
    {
        if (Request["action"] != null)
        {
            e.AddingValues["EmployeeID"] = Request["eid"].ToString();
        }
        else
        {
            Control_SelUser su = Contract.FindControl("SelUser") as Control_SelUser;
            e.AddingValues["EmployeeID"] = su.EmployeeId;


            hidEname.Value = su.EmpName;
            hidEid.Value = su.EmployeeId;

        }

        e.AddingValues["EditEmpId"] = Convert.ToInt32(Srvtools.CliUtils.GetValue("_UserCode")[1]);
    }
    protected void Master_Updating(object sender, Srvtools.WebDataSourceUpdatingEventArgs e)
    {

        e.UpdatingValues["EditEmpId"] = Convert.ToInt32(Srvtools.CliUtils.GetValue("_UserCode")[1]);
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Response.Redirect(Server.UrlDecode(Request["redurl"]));
    }
}
